'''
Created on 18.7.2011

@author: gilead
'''

from panda3d.core import NodePath #@UnresolvedImport

from ShipFactory import ShipFactory

class World(NodePath):
    '''
    classdocs
    '''
    def __init__(self, *args, **kwargs):
        '''
        Constructor
        '''
        NodePath.__init__(self, *args, **kwargs)
        self.userID = None
    
    def setUserID(self, id):
        self.userID = id
    
    def FindShip(self, guid):
        for obj in self.getChildren():
            ship = obj.getPythonTag('Ship')
            if ship != None:
                if ship.guid == guid:
                    return ship
        return None
    
    def MoveShip(self, guid, x, y, vel, heading):
        ship = self.FindShip(guid)
        if ship != None:
            ship.setPos(x, y, 0)
            ship.setVelocity(vel)
            ship.setH(heading)
    
    def CreateShip(self, x, y, z, guid, ownerid, typeid=1):
        if self.FindShip(guid) != None:
            print "Tried to create a ship with already existing ID!"
            return
        
        ship = ShipFactory.CreateShip(x,y,z,guid,ownerid,typeid)
        ship.reparentTo(self)
        
        if ship.ownerid == self.userID:
            messenger.send("own_ship_created", [ship])
        else:
            messenger.send("not_own_ship_created", [ship])